﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class EditPhone

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.PhoneRepository = PhDir.Main.Instance.PhoneRepository
    End Sub

    Private Property PhoneRepository As PhoneRepository

    Public Overloads Sub Show(ByVal userId As String, ByVal oldPhone As String)
        Me.Visible = True
        If Not String.IsNullOrEmpty(userId) And Not String.IsNullOrEmpty(oldPhone) Then
            Me.UserIdTextBox.Text = userId
            Me.UserIdTextBox.Enabled = False

            Me.OldPhoneTextBox.Text = oldPhone
            Me.OldPhoneTextBox.Enabled = False
        End If
    End Sub

    Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
        If Me.UserIdTextBox.Enabled = True Then
            Me.UserIdTextBox.Text = String.Empty
            Me.OldPhoneTextBox.Text = String.Empty
        End If
        Me.NewPhoneMaskedBox.Text = String.Empty
    End Sub

    Private Sub EditPhone_VisibleChanged(sender As System.Object, e As System.EventArgs) Handles MyBase.VisibleChanged
        Me.UserIdTextBox.Enabled = True
        Me.OldPhoneTextBox.Enabled = True
        Me.ResetButton_Click(Me, EventArgs.Empty)
    End Sub

    Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
        Dim oldPhone = New Phone With {.PhoneNumber = Me.OldPhoneTextBox.Text}
        Dim newPhone = New Phone With {.PhoneNumber = Me.NewPhoneMaskedBox.Text}
        Dim isSuccess = Me.PhoneRepository.Update(oldPhone, newPhone)

        If isSuccess = True Then
            MessageBox.Show("Operation Succeed")
        Else
            MessageBox.Show("Operation Failed")
        End If
    End Sub
End Class
